/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.QuestionTypeDTO;

/**
 *
 * @author User
 */
public class QuestionTypeServiceImpl implements IEntity{
	
	Statement stmt=null;
    ResultSet rst=null;
    private QuestionTypeDTO questionType;
    private List<QuestionTypeDTO> questionTypeList;

    public QuestionTypeDTO GetById(int id) {
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from questiontype where idQuestionType=" + id); //select resultset
            questionType.setIdQuestionType(id);
            questionType.setQuestionTypeTitle(rst.getString("QuestionTypeTitle"));
            questionType.setQuestionTypeDesc(rst.getString("QuestionTypeDesc"));
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(QuestionTypeServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         return questionType;
    }

    public List<QuestionTypeDTO> GetAll() {
        
    	try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from questiontype "); 
            while (rst.next()) {
                questionType.setIdQuestionType(rst.getInt("idQuestionType"));
                questionType.setQuestionTypeTitle(rst.getString("QuestionTypeTitle"));
                questionType.setQuestionTypeDesc(rst.getString("QuestionTypeDesc"));
                questionTypeList.add(questionType);
            }
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(QuestionTypeServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

      return questionTypeList;
    }
    public void Add(Object obj) {
    	
       QuestionTypeDTO objCast=(QuestionTypeDTO)obj;
       try {
    	   
    	   stmt =  Configuration.GetStatement();
    	   stmt.execute("INSERT INTO dbsurvey.questiontype ( `QuestionTypeTitle`, `QuestionTypeDesc`) VALUES ("+objCast.getQuestionTypeTitle()+","+objCast.getQuestionTypeDesc()+")");
    	   Configuration.CloseConnection();
       
	   	} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void Update(Object obj) {
        QuestionTypeDTO objCast=(QuestionTypeDTO)obj;
        
        try {
			
        	stmt = Configuration.GetStatement();
        	stmt.execute("update dbsurvey.questiontype set  QuestionTypeTitle="+objCast.getQuestionTypeTitle()+"and QuestionTypeDesc="+objCast.getQuestionTypeDesc()+" where idQuestionType="+objCast.getIdQuestionType());
        	Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Remove(int id) {
        try {
			
        	stmt = Configuration.GetStatement();
        	stmt.execute("delete from questiontype where idQuestionType =" + id);
        	Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }


}
